﻿using System;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
using static AD域工具.Tool.API;

namespace AD域工具.Tool
{

    #region 计算机帮助类
    /// <summary>
    /// 计算机帮助类
    /// </summary>
    #endregion
    class ComputerHelper
    {
        #region 程序运行目录
        /// <summary>
        /// 程序运行目录
        /// </summary>
        #endregion
        public static string RunPath
        {
            get
            {
                return System.AppDomain.CurrentDomain.BaseDirectory;
            }
        }

        #region 设置域名称
        /// <summary>
        /// 设置域名称
        /// </summary>
        /// <param name="doaminNmae">要设置的域名称</param>
        /// <returns></returns>
        #endregion
        public static bool SetDomainName(string doaminNmae)
        {
            return SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsDomain, doaminNmae);
        }

        #region 获取域名称
        /// <summary>
        /// 获取域名称
        /// </summary>
        /// <returns></returns>
        #endregion
        public static string GetDomainName()
        {
            return GetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsDomain);
        }

        #region 设置计算机名称
        /// <summary>
        /// 设置计算机名称
        /// </summary>
        /// <param name="computerNmae">计算机名称</param>
        /// <returns></returns>
        #endregion
        public static bool SetComputerName(string computerNmae)
        {
            return SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, computerNmae);
        }

        #region 生成注册表代码
        /// <summary>
        /// 生成注册表代码
        /// </summary>
        /// <param name="path">注册表路由</param>
        /// <param name="node">节点</param>
        /// <param name="value">修改的内容</param>
        /// <returns></returns>
        #endregion
        private static string MakeRegeditCode(string path, string node, string value) {
            return $@"reg add ""{path}"" /v ""{node}"" /t reg_sz /d ""{value}"" /f >nul 2>nul";
        }

        #region 设置计算机名称_注册表
        /// <summary>
        /// 设置计算机名称_注册表
        /// </summary>
        /// <param name="computerNmae">计算机名称</param>
        /// <returns></returns>
        #endregion
        public static bool SetComputerNameByRegedit(string computerNmae)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(MakeRegeditCode(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName", "ComputerName", computerNmae));
            p.StandardInput.WriteLine(MakeRegeditCode(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "NV Hostname", computerNmae));
            p.StandardInput.WriteLine(MakeRegeditCode(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "Hostname", computerNmae));
            p.StandardInput.WriteLine("exit");
            p.Close();
            return true;
        }

        #region 获取计算机名称
        /// <summary>
        /// 获取计算机名称
        /// </summary>
        /// <returns></returns>
        #endregion
        public static string GetComputerName()
        {
            return GetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname);
        }

        #region 获取当前用户的目录
        /// <summary>
        /// 获取当前用户的目录
        /// </summary>
        /// <returns></returns>
        #endregion
        public static string GetUserFolder() {
            string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
            if (Environment.OSVersion.Version.Major >= 6)
            {
                path = Directory.GetParent(path).ToString();
            }
            return path;
        }

        #region 判断程序是否是以管理员身份运行
        /// <summary>
        /// 判断程序是否是以管理员身份运行。
        /// </summary>
        #endregion
        public static bool IsRunAsAdmin()
        {
            WindowsIdentity id = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(id);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        #region 打开目录
        /// <summary>
        /// 打开目录
        /// </summary>
        /// <param name="path">要打开的目录路径</param>
        #endregion
        public static void OpenDirInExplorer(string path) { 
            Process.Start(path);
        }

        #region 打开文件位置
        /// <summary>
        /// 打开文件位置
        /// </summary>
        /// <param name="filePath">要查看的文件</param>
        #endregion
        public static void OpenFileInExplorer(string filePath)
        {
            Process.Start("explorer", "/select,\"" + filePath + "\"");
        }
    }
}
